﻿using Entity.Models;
using HuaRunInterface.Comm;
using HuaRunInterface.Models;
using Microsoft.AspNetCore.Mvc;
using Utility;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaRunInterface.Controllers.GetControllers
{
    [Route("api/HuaRun/[controller]")]
    [ApiController]
    public class GetRTUInfoController : ControllerBase
    {
        // POST api/<GetRTUInfoController>
        [HttpPost]
        public object Post([FromBody] GetRtuParamModel value)
        {
            return GetDatas(value);
        }

        private object GetDatas(GetRtuParamModel dto)
        {
            ReturnData rd = new ReturnData();

            try
            {
                string conn = Global.Configuration["ConnectionStrings:DDb"];
                using (Entity.DBContext context = Entity.BaseContext.CreateContext(conn))
                {
                    int skip = dto.PageSize * (dto.PageIndex - 1);
                    rd.Total = (from p in context.rTUInfos
                                where string.IsNullOrWhiteSpace(dto.deviceCode) || p.deviceCode.Contains(dto.deviceCode)
                                where string.IsNullOrWhiteSpace(dto.protocolTypeCode) || p.protocolTypeCode == dto.protocolTypeCode
                                where string.IsNullOrWhiteSpace(dto.commMode) || p.commMode == dto.commMode
                                where string.IsNullOrWhiteSpace(dto.deviceType) || p.deviceType == dto.deviceType
                                where p.IsDelete != 1
                                select p).Count();
                    List<RTUInfo> list = (from p in context.rTUInfos
                                          where string.IsNullOrWhiteSpace(dto.deviceCode) || p.deviceCode.Contains(dto.deviceCode)
                                          where string.IsNullOrWhiteSpace(dto.protocolTypeCode) || p.protocolTypeCode == dto.protocolTypeCode
                                          where string.IsNullOrWhiteSpace(dto.commMode) || p.commMode == dto.commMode
                                          where string.IsNullOrWhiteSpace(dto.deviceType) || p.deviceType == dto.deviceType
                                          where p.IsDelete != 1
                                          select p).SelfOrderBy(dto.OrderBy, dto.IsDesc).Skip(skip).Take(dto.PageSize).ToList();
                    if (list != null && list.Count > 0)
                    {
                        var v = (from p in list                                 
                                 join pt in Global.ProtocolTypes_Industry on p.protocolTypeCode equals pt.Key into pt1
                                 from ppt in pt1.DefaultIfEmpty()
                                 join c in Global.CommunicationType_Industry on p.commMode equals c.Key into c1
                                 from cc in c1.DefaultIfEmpty()
                                 join d in Global.DeviceType_Industry on p.deviceType equals d.Key into d1
                                 from dd in d1.DefaultIfEmpty()
                                 select new
                                 {
                                     p,
                                     ProtocolTypeCodeName = ppt?.Value,
                                     commModeName=cc?.Value,
                                     deviceTypeName=dd?.Value
                                 });
                        rd.Data = v;
                    }
                    else
                    {
                        rd.Data = list;
                    }
                }
            }
            catch (Exception ex)
            {
                rd.State = "failed";
                rd.Message = ex.Message;
                rd.Data = ex.StackTrace;
            }
            return rd;
        }
    }

    public class GetRtuParamModel : GetBaseParamModels
    {
        public string? deviceCode { get; set; }
        public string? protocolTypeCode { get; set; }
        public string? commMode { get; set; }
        public string? deviceType { get; set; }
    }
}
